<?php

/**
 * Description of ContentUserChange
 *
 * @author Honza, Kevin
 */
class ContentUserChange implements IMainBodyWithInteraction {

  private $formMessage;				//display messages
  private $login;      				//login details
  private $firstname;				//user firstname 
  private $surname;					//user surname 
  private $street;					//field of street 
  private $cp;
  private $city;
  private $postcode;
  private $email;
  private $mobile;
  private $userID;

  public function __construct() {
	  //get author instance 
    $auth = Auth::getInstance();
		//identify  login and user id
    $identity = $auth->getIdentity();
    $this->login = $identity['login'];
    $this->userID = $identity['id'];
  }
  
  public function action() {
    
  }

  public function applyForm($formID = NULL) {
	  //edit user details
    if (isset($_POST['editDetails'])) {
      $pass = addslashes($_POST['pass']);
      $login = $this->login;
      $firstname = addslashes($_POST['name']);
      $surname = addslashes($_POST['surname']);
      $street = addslashes($_POST['street']);
      $cp = addslashes($_POST['cp']);
      $city = addslashes($_POST['city']);
      $postcode = addslashes($_POST['postcode']);
      $email = addslashes($_POST['email']);
      $mobile = addslashes($_POST['mobile']);

      if (!$firstname || !$surname || !$email || !$city) { // all column have to complete
        $this->formMessage .="<p class=\"error\">You didn´t fill all compulsory fields</p>";
      } else {
      
        if (!filter_var("$email", FILTER_VALIDATE_EMAIL)) {
          $this->formMessage.="<p class=\"error\">Email isn´t valid</p>";
          return;
        }
      
        $sql = 'update ' . TBL_USERS . " set ";
        $sql .= "name='$firstname', surname='$surname', email='$email', ";
        $sql .= "mobile='$mobile' where id_user='$this->userID'";
        DB::sql($sql);

        //pass update
        if ($pass) {

          $hPass = Auth::encrypt($pass, $login);
          $sql = 'update ' . TBL_USERS . " set pass='$hPass' where id_user='$this->userID'";
          DB::sql($sql);
        }

        //address update
        $sql = 'select * from ' . TBL_USERS . " where id_user='$this->userID'";
        $user = DB::sql($sql);

        $sql = 'update ' . TBL_ADDRESSES . " set street='$street', cp='$cp', city='$city', postal_code='$postcode'";
        $sql .= ' where id_address=' . $user[0]['id_address'];
        DB::sql($sql);
      }
    }
  }
//getContent function called automatically
  public function getContent() {
    $content = '';
    $content .= '<h2>Edit details</h2>';

    $this->applyForm();
    
    $content .= $this->formMessage;
    $content .= $this->getForm();

    return $content;
  }
// join table from users with roles and addresses 
  public function getForm($formID = NULL, $error = NULL) {
    $sql = 'select * from ' . TBL_USERS . '';
    $sql .= ' join ' . TBL_ROLES . ' using(id_role)';
    $sql .= ' join ' . TBL_ADDRESSES . ' using(id_address)';
    $sql .= ' where id_user = ' . $this->userID;
    $result = DB::sql($sql);

    $this->firstname = $result[0]['name'];
    $this->surname = $result[0]['surname'];
    $this->street = $result[0]['street'];
    $this->cp = $result[0]['cp'];
    $this->city = $result[0]['city'];
    $this->postcode = $result[0]['postal_code'];
    $this->email = $result[0]['email'];
    $this->mobile = $result[0]['mobile'];

    $f = new Form();
    $f->startForm("self", "post", null, array("class" => "form_settings"));
    $f->addDescription("Change password");
    $f->addLabel("Password", "pass")->addInput("password", "pass");
    $f->addDescription("Personal information");
    $f->addLabel("Name*", "name")->addInput("text", "name", $this->firstname);
    $f->addLabel("Surname*", "surname")->addInput("text", "surname", $this->surname);
    $f->addLabel("Email*", "email")->addInput("text", "email", $this->email);
    $f->addLabel("Mobile", "mobile")->addInput("text", "mobile", $this->mobile);
    $f->addDescription("Address");
    $f->addLabel("Street", "street")->addInput("text", "street", $this->street);
    $f->addLabel("Street number", "cp")->addInput("text", "cp", $this->cp);
    $f->addLabel("City*", "city")->addInput("text", "city", $this->city);
    $f->addLabel("Postal code", "postcode")->addInput("text", "postcode", $this->postcode);
    $f->addInput('hidden', 'ida', $_GET['ida']);
    $f->addLabel("&nbsp;", "")->addInput("submit", "editDetails", "Edit", array("class" => "submit"));
    $f->endForm();
    $form = $f->getForm();
    return $form;
  }

  public function getSubmitedForm() {
    
  }

  public function getTable($tableID = NULL) {
    
  }
}
?>
